package com.chuang.server.controller;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chuang.server.pojo.ResultBean;
import com.chuang.server.pojo.ResultPageBean;
import com.chuang.server.pojo.User;
import com.chuang.server.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author vicente
 * @since 2021-12-14
 */
@Controller
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    /*
    * 查询所有用户
    * */
    @ResponseBody
    @CrossOrigin(origins = "*")

    @GetMapping("/getUserList")
    public ResultBean getUserList(){
        try{
            List<Map<Object,Object>> maps = userService.selectAll();
            return  ResultBean.success("成功",maps);

        }catch (Exception e){
            return ResultBean.error("失败",e);
        }

    }

    /*
    * 根据id查询单个用户
    * */
    @ResponseBody
    @GetMapping("/getUserById")
    public ResultBean getUserById(@RequestBody Map<String,Integer> params){

        try{
            int id = params.get("id");
            Map map = userService.selectUserById(id);
            if(map == null){
                return ResultBean.success("查询失败", new HashMap<>());
            }else {
                return ResultBean.success("查询成功",map);
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResultBean.success("查询失败",e);
        }

    }

    /*
    * 新增用户
    * */
    @ResponseBody
    @PostMapping("/addUser")
    public ResultBean addUser(@RequestBody User user){
        try{
            int i = userService.addUser(user);
            if(i!=0){
                return ResultBean.success("新增用户成功");
            }else {
                return ResultBean.success("新增用户失败");
            }

        }catch (Exception e){
            return ResultBean.success("新增用户失败",e);
        }

    }
    /*
    * 根据id 删除用户
    * */
    @ResponseBody
    @PostMapping("/deleteUser")
    public ResultBean deleteUser(@RequestBody JSONObject jsonObject){
        try{
            Integer id = jsonObject.getInteger("id");
            userService.delUserById(id);
            return ResultBean.success("删除成功",id);
        }catch (Exception e){
            e.printStackTrace();
            return ResultBean.error("删除失败");
        }
    }
    /**
     * 分页查询数据
     */
    @ResponseBody
    @PostMapping("/pageList")
    public ResultPageBean pageList(@RequestBody JSONObject jsonObject){
        Integer page = jsonObject.getInteger("page");
        Integer pageSize = jsonObject.getInteger("pageSize");

        Page<User> userPage = new Page<>(page, pageSize);
        return  userService.selectUserPage(userPage, 0);
    }
}
